//
//  QYMoreNotificationsCell.m
//  QingYue
//
//  Created by Zheng on 2017/6/23.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYMoreNotificationsCell.h"
#import "QYMoreNotificationsModel.h"

@implementation QYMoreNotificationsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [self.notiBtn setImage:[UIImage imageNamed:@"qy_lb_n"] forState:UIControlStateSelected];
}

- (void)setModel:(QYMoreNotificationsModel *)model {
    _model = model;
    
    _timeLbl.text = _model.PostTime;
    _titleLbl.text = _model.NewsContent;
    _sendLbl.text = [NSString stringWithFormat:@"发送人:  %@",model.PsotID];
    
    if (_model.HasReply.integerValue == 8) {
        _cancelBtn.hidden = YES;
    } else {
        _cancelBtn.hidden = NO;
    }
    
    switch (_model.HasReply.integerValue) {
        case 0:
        {
            _noBtn.hidden = YES;
            _yesBtn.hidden = YES;
            _notiBtn.selected = YES;
        }
            break;
        case 1:
        case 2:
        {
            _noBtn.hidden = NO;
            _yesBtn.hidden = NO;
            _notiBtn.selected = NO;
        }
            break;
            
        default:
            break;
    }
}

//回复
- (void)receiveRequestWithParam:(NSDictionary *)param {
    [[YCRequestDataService sharedManager] Post:@"ReplyNews" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            
            [SVProgressHUD dismissWithDelay:.35];

            _block();
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}

//确认
- (void)sureRequestWithParam:(NSDictionary *)param {
    [[YCRequestDataService sharedManager] Post:@"Confirm" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            
            [SVProgressHUD dismissWithDelay:.35];

            _block();
            
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}

- (IBAction)yesBtnAction:(id)sender {
    
    [SVProgressHUD show];

    NSDictionary *param = @{@"id":_model.ID,
                            @"Type":@1,
                            @"username":[kUserDefaults objectForKey:@"username"]};

    if (_model.HasReply.integerValue == 1) {
        [self receiveRequestWithParam:param];
    } else {
        [self sureRequestWithParam:param];
    }
}


- (IBAction)noBtnAction:(id)sender {
    [SVProgressHUD show];

    if (_model.HasReply.integerValue == 1) {
        NSDictionary *param = @{@"id":_model.ID,
                                @"Type":@2,
                                @"username":[kUserDefaults objectForKey:@"username"]};
        [self receiveRequestWithParam:param];
    } else {
        NSDictionary *param = @{@"id":_model.ID,
                                @"Type":@0,
                                @"username":[kUserDefaults objectForKey:@"username"]};
        [self sureRequestWithParam:param];
    }
}


- (IBAction)cancelBtnAction:(UIButton *)sender {
    [SVProgressHUD show];

    [[YCRequestDataService sharedManager] Post:@"DelNews" parameters:@{@"id":_model.ID} success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            
            [SVProgressHUD dismissWithDelay:.35];

            _block();
            
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];
}

- (void)reloadDataAction:(void (^)())block {
    _block = block;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
